<commit_before>//// This file is a part of the OpenSurgSim project.
//// Copyright 2013, SimQuest Solutions Inc.
////
//// Licensed under the Apache License, Version 2.0 (the "License");
//// you may not use this file except in compliance with the License.
//// You may obtain a copy of the License at
////
////     http://www.apache.org/licenses/LICENSE-2.0
////
//// Unless required by applicable law or agreed to in writing, software
//// distributed under the License is distributed on an "AS IS" BASIS,
//// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//// See the License for the specific language governing permissions and
//// limitations under the License.

#include <time.h>

#include <gtest/gtest.h>

#include <SurgSim/Math/Vector.h>

#include <SurgSim/Physics/Actors/Shapes.h>

/* CUBE
     3*----------*2
     /          /|
   7*----------* |
    |         6| |
    | *0       | *1
    |          |/
   4*----------*5
*/
static const int cubeNumPoints = 8;
static const SurgSim::Math::Vector3d cubePoints[8]=
{
	SurgSim::Math::Vector3d(-1.0/2.0, -1.0/2.0, -1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0, -1.0/2.0, -1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0,  1.0/2.0, -1.0/2.0),
	SurgSim::Math::Vector3d(-1.0/2.0,  1.0/2.0, -1.0/2.0),

	SurgSim::Math::Vector3d(-1.0/2.0, -1.0/2.0,  1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0, -1.0/2.0,  1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0,  1.0/2.0,  1.0/2.0),
	SurgSim::Math::Vector3d(-1.0/2.0,  1.0/2.0,  1.0/2.0)
};

static const int cubeNumEdges = 12;
static const int cubeEdges[12][2] =
{
	{0, 1}, {3, 2}, {4, 5}, {7, 6}, // +X
	{0, 3}, {1, 2}, {4, 7}, {5 ,6}, // +Y
	{0, 4}, {1, 5}, {2, 6}, {3, 7}  // +Z
};

static const int cubeNumTriangles = 12;
static const int cubeTrianglesCCW[12][3] =
{
	{6, 2, 3}, {6, 3, 7}, // Top    ( 0  1  0) [6237]
	{0, 1, 5}, {0, 5, 4}, // Bottom ( 0 -1  0) [0154]
	{4, 5, 6}, {4, 6, 7}, // Front  ( 0  0  1) [4567]
	{0, 3, 2}, {0, 2, 1}, // Back   ( 0  0 -1) [0321]
	{1, 2, 6}, {1, 6, 5}, // Right  ( 1  0  0) [1265]
	{0, 4, 7}, {0, 7, 3}  // Left   (-1  0  0) [0473]
};

class EmptyData
{
public:
	bool operator ==(const EmptyData& e) const { return true; };
};

class CubeMeshTest : public ::testing::Test
{
public:
	typedef SurgSim::DataStructures::TriangleMesh<EmptyData,EmptyData,EmptyData> TriangleMesh;
	typedef SurgSim::DataStructures::MeshElement<2,EmptyData> EdgeElement;
	typedef SurgSim::DataStructures::MeshElement<3,EmptyData> TriangleElement;

	void SetUp()
	{
		numIterations = 100;

		srand((unsigned int)time(nullptr));
	}

	void TearDown()
	{
	}

	/// Number of iterations to test
	int numIterations;
};

TEST_F(CubeMeshTest, MeshCubeVSBoxTest)
{
	for (int iterationID = 0; iterationID < numIterations; iterationID++)
	{
		double density = 1000.0 * (double)rand()/(double)RAND_MAX + 1.0;
		double lx = 10.0 * (double)rand()/(double)RAND_MAX + 1.0;
		double ly = 10.0 * (double)rand()/(double)RAND_MAX + 1.0;
		double lz = 10.0 * (double)rand()/(double)RAND_MAX + 1.0;

		// Cube
		std::shared_ptr<TriangleMesh> mesh = std::make_shared<TriangleMesh>();
		for (int i = 0; i < cubeNumPoints; i++)
		{
			EmptyData emptyData;
			SurgSim::Math::Vector3d p;
			p[0] = cubePoints[i][0] * lx;
			p[1] = cubePoints[i][1] * ly;
			p[2] = cubePoints[i][2] * lz;
			TriangleMesh::Vertex v(p, emptyData);
			mesh->addVertex(v);
		}
		for (int i = 0; i < cubeNumEdges; i++)
		{
			EmptyData emptyData;
			std::array<unsigned int,2> edgePoints;
			for (int j = 0; j < 2; j++)
			{
				edgePoints[j] = cubeEdges[i][j];
			}
			EdgeElement edgeElement(edgePoints, emptyData);
			TriangleMesh::Edge e(edgeElement);
			mesh->addEdge(e);
		}
		for (int i = 0; i < cubeNumTriangles; i++)
		{
			EmptyData emptyData;
			std::array<unsigned int,3> trianglePoints;
			for (int j = 0; j < 3; j++)
			{
				trianglePoints[j] = cubeTrianglesCCW[i][j];
			}
			TriangleElement triangleElement(trianglePoints, emptyData);
			TriangleMesh::Triangle t(triangleElement);
			mesh->addTriangle(t);
		}

		SurgSim::Physics::MeshShape<EmptyData, EmptyData, EmptyData> boxMesh(mesh);

		SurgSim::Physics::BoxShape boxShape(lx, ly, lz);

		SurgSim::Math::Matrix33d expectedInertia;
		SurgSim::Math::Vector3d expectedMassCenter;
		double expectedMass;
		expectedMass       = boxShape.calculateMass(density);
		expectedInertia    = boxShape.calculateInertia(density);
		expectedMassCenter = boxShape.calculateMassCenter();

		EXPECT_NEAR(boxShape.calculateVolume(), boxMesh.calculateVolume(), 1e-8);
		EXPECT_EQ(expectedMassCenter, boxMesh.calculateMassCenter());
		EXPECT_NEAR(expectedMass, boxMesh.calculateMass(density), 1e-8);
		EXPECT_TRUE(boxMesh.calculateInertia(density).isApprox(expectedInertia));
	}
}
<commit_msg>Class variable renaming from X to m_X<commit_after>//// This file is a part of the OpenSurgSim project.
//// Copyright 2013, SimQuest Solutions Inc.
////
//// Licensed under the Apache License, Version 2.0 (the "License");
//// you may not use this file except in compliance with the License.
//// You may obtain a copy of the License at
////
////     http://www.apache.org/licenses/LICENSE-2.0
////
//// Unless required by applicable law or agreed to in writing, software
//// distributed under the License is distributed on an "AS IS" BASIS,
//// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//// See the License for the specific language governing permissions and
//// limitations under the License.

#include <time.h>

#include <gtest/gtest.h>

#include <SurgSim/Math/Vector.h>

#include <SurgSim/Physics/Actors/Shapes.h>

/* CUBE
     3*----------*2
     /          /|
   7*----------* |
    |         6| |
    | *0       | *1
    |          |/
   4*----------*5
*/
static const int cubeNumPoints = 8;
static const SurgSim::Math::Vector3d cubePoints[8]=
{
	SurgSim::Math::Vector3d(-1.0/2.0, -1.0/2.0, -1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0, -1.0/2.0, -1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0,  1.0/2.0, -1.0/2.0),
	SurgSim::Math::Vector3d(-1.0/2.0,  1.0/2.0, -1.0/2.0),

	SurgSim::Math::Vector3d(-1.0/2.0, -1.0/2.0,  1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0, -1.0/2.0,  1.0/2.0),
	SurgSim::Math::Vector3d( 1.0/2.0,  1.0/2.0,  1.0/2.0),
	SurgSim::Math::Vector3d(-1.0/2.0,  1.0/2.0,  1.0/2.0)
};

static const int cubeNumEdges = 12;
static const int cubeEdges[12][2] =
{
	{0, 1}, {3, 2}, {4, 5}, {7, 6}, // +X
	{0, 3}, {1, 2}, {4, 7}, {5 ,6}, // +Y
	{0, 4}, {1, 5}, {2, 6}, {3, 7}  // +Z
};

static const int cubeNumTriangles = 12;
static const int cubeTrianglesCCW[12][3] =
{
	{6, 2, 3}, {6, 3, 7}, // Top    ( 0  1  0) [6237]
	{0, 1, 5}, {0, 5, 4}, // Bottom ( 0 -1  0) [0154]
	{4, 5, 6}, {4, 6, 7}, // Front  ( 0  0  1) [4567]
	{0, 3, 2}, {0, 2, 1}, // Back   ( 0  0 -1) [0321]
	{1, 2, 6}, {1, 6, 5}, // Right  ( 1  0  0) [1265]
	{0, 4, 7}, {0, 7, 3}  // Left   (-1  0  0) [0473]
};

class EmptyData
{
public:
	bool operator ==(const EmptyData& e) const { return true; };
};

class CubeMeshTest : public ::testing::Test
{
public:
	typedef SurgSim::DataStructures::TriangleMesh<EmptyData,EmptyData,EmptyData> TriangleMesh;
	typedef SurgSim::DataStructures::MeshElement<2,EmptyData> EdgeElement;
	typedef SurgSim::DataStructures::MeshElement<3,EmptyData> TriangleElement;

	void SetUp()
	{
		m_numIterations = 100;

		srand((unsigned int)time(nullptr));
	}

	void TearDown()
	{
	}

	/// Number of iterations to test
	int m_numIterations;
};

TEST_F(CubeMeshTest, MeshCubeVSBoxTest)
{
	for (int iterationID = 0; iterationID < m_numIterations; iterationID++)
	{
		double density = 1000.0 * (double)rand()/(double)RAND_MAX + 1.0;
		double lx = 10.0 * (double)rand()/(double)RAND_MAX + 1.0;
		double ly = 10.0 * (double)rand()/(double)RAND_MAX + 1.0;
		double lz = 10.0 * (double)rand()/(double)RAND_MAX + 1.0;

		// Cube
		std::shared_ptr<TriangleMesh> mesh = std::make_shared<TriangleMesh>();
		for (int i = 0; i < cubeNumPoints; i++)
		{
			EmptyData emptyData;
			SurgSim::Math::Vector3d p;
			p[0] = cubePoints[i][0] * lx;
			p[1] = cubePoints[i][1] * ly;
			p[2] = cubePoints[i][2] * lz;
			TriangleMesh::Vertex v(p, emptyData);
			mesh->addVertex(v);
		}
		for (int i = 0; i < cubeNumEdges; i++)
		{
			EmptyData emptyData;
			std::array<unsigned int,2> edgePoints;
			for (int j = 0; j < 2; j++)
			{
				edgePoints[j] = cubeEdges[i][j];
			}
			EdgeElement edgeElement(edgePoints, emptyData);
			TriangleMesh::Edge e(edgeElement);
			mesh->addEdge(e);
		}
		for (int i = 0; i < cubeNumTriangles; i++)
		{
			EmptyData emptyData;
			std::array<unsigned int,3> trianglePoints;
			for (int j = 0; j < 3; j++)
			{
				trianglePoints[j] = cubeTrianglesCCW[i][j];
			}
			TriangleElement triangleElement(trianglePoints, emptyData);
			TriangleMesh::Triangle t(triangleElement);
			mesh->addTriangle(t);
		}

		SurgSim::Physics::MeshShape<EmptyData, EmptyData, EmptyData> boxMesh(mesh);

		SurgSim::Physics::BoxShape boxShape(lx, ly, lz);

		SurgSim::Math::Matrix33d expectedInertia;
		SurgSim::Math::Vector3d expectedMassCenter;
		double expectedMass;
		expectedMass       = boxShape.calculateMass(density);
		expectedInertia    = boxShape.calculateInertia(density);
		expectedMassCenter = boxShape.calculateMassCenter();

		EXPECT_NEAR(boxShape.calculateVolume(), boxMesh.calculateVolume(), 1e-8);
		EXPECT_EQ(expectedMassCenter, boxMesh.calculateMassCenter());
		EXPECT_NEAR(expectedMass, boxMesh.calculateMass(density), 1e-8);
		EXPECT_TRUE(boxMesh.calculateInertia(density).isApprox(expectedInertia));
	}
}
<|endoftext|>


<commit_before>
#include "ImwPlatformWindow.h"

#include "ImwWindowManager.h"
#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>

using namespace ImWindow;

ImwPlatformWindow::ImwPlatformWindow(bool bMain, bool bIsDragWindow)
{
	m_bMain = bMain;
	m_bIsDragWindow = bIsDragWindow;
	m_pContainer = new ImwContainer(this);
	m_pState = NULL;
	m_pPreviousState = NULL;

	void* pTemp = ImGui::GetInternalState();
	m_pState = ImwMalloc(ImGui::GetInternalStateSize());
	memcpy(m_pState, ImGui::GetInternalState(), ImGui::GetInternalStateSize());
	ImGui::SetInternalState(m_pState);
	//ImGui::SetInternalState(m_pState, true);
	ImGui::SetInternalState(pTemp);
}

ImwPlatformWindow::~ImwPlatformWindow()
{
	ImwSafeDelete(m_pContainer);
	ImwSafeDelete(m_pState);
}

void ImwPlatformWindow::OnClose()
{
	ImwWindowManager::GetInstance()->OnClosePlatformWindow(this);
}

static bool s_bStatePush = false;
void ImwPlatformWindow::SetState()
{
	ImwAssert(s_bStatePush == false);
	s_bStatePush = true;
	m_pPreviousState = ImGui::GetInternalState();
	ImGui::SetInternalState(m_pState);
	memcpy(&((ImGuiState*)m_pState)->Style, &((ImGuiState*)m_pPreviousState)->Style, sizeof(ImGuiStyle));
}

void ImwPlatformWindow::RestoreState()
{
	ImwAssert(s_bStatePush == true);
	s_bStatePush = false;
	memcpy(&((ImGuiState*)m_pPreviousState)->Style, &((ImGuiState*)m_pState)->Style, sizeof(ImGuiStyle));
	ImGui::SetInternalState(m_pPreviousState);
}

void ImwPlatformWindow::OnLoseFocus()
{
	ImGuiState& g = *((ImGuiState*)m_pState);
	g.SetNextWindowPosCond = g.SetNextWindowSizeCond = g.SetNextWindowContentSizeCond = g.SetNextWindowCollapsedCond = g.SetNextWindowFocus = 0;
}

void ImwPlatformWindow::Paint()
{
	ImwWindowManager::GetInstance()->Paint(this);
}

bool ImwPlatformWindow::IsMain()
{
	return m_bMain;
}

void ImwPlatformWindow::Dock(ImwWindow* pWindow)
{
	m_pContainer->Dock(pWindow);
}

bool ImwPlatformWindow::UnDock(ImwWindow* pWindow)
{
	return m_pContainer->UnDock(pWindow);
}

ImwContainer* ImwPlatformWindow::GetContainer()
{
	return m_pContainer;
}

ImwContainer* ImwPlatformWindow::HasWindow(ImwWindow* pWindow)
{
	return m_pContainer->HasWindow(pWindow);
}

void ImwPlatformWindow::PaintContainer()
{
	m_pContainer->Paint();
}
<commit_msg>Fix Platform Window states, not initilialized and crash sometimes<commit_after>
#include "ImwPlatformWindow.h"

#include "ImwWindowManager.h"
#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>

using namespace ImWindow;

ImwPlatformWindow::ImwPlatformWindow(bool bMain, bool bIsDragWindow)
{
	m_bMain = bMain;
	m_bIsDragWindow = bIsDragWindow;
	m_pContainer = new ImwContainer(this);
	m_pState = NULL;
	m_pPreviousState = NULL;

	void* pTemp = ImGui::GetInternalState();
	m_pState = ImwMalloc(ImGui::GetInternalStateSize());
	ImGui::SetInternalState(m_pState, true);
	ImGui::SetInternalState(pTemp);
}

ImwPlatformWindow::~ImwPlatformWindow()
{
	ImwSafeDelete(m_pContainer);
	ImwSafeDelete(m_pState);
}

void ImwPlatformWindow::OnClose()
{
	ImwWindowManager::GetInstance()->OnClosePlatformWindow(this);
}

static bool s_bStatePush = false;
void ImwPlatformWindow::SetState()
{
	ImwAssert(s_bStatePush == false);
	s_bStatePush = true;
	m_pPreviousState = ImGui::GetInternalState();
	ImGui::SetInternalState(m_pState);
	memcpy(&((ImGuiState*)m_pState)->Style, &((ImGuiState*)m_pPreviousState)->Style, sizeof(ImGuiStyle));
}

void ImwPlatformWindow::RestoreState()
{
	ImwAssert(s_bStatePush == true);
	s_bStatePush = false;
	memcpy(&((ImGuiState*)m_pPreviousState)->Style, &((ImGuiState*)m_pState)->Style, sizeof(ImGuiStyle));
	ImGui::SetInternalState(m_pPreviousState);
}

void ImwPlatformWindow::OnLoseFocus()
{
	ImGuiState& g = *((ImGuiState*)m_pState);
	g.SetNextWindowPosCond = g.SetNextWindowSizeCond = g.SetNextWindowContentSizeCond = g.SetNextWindowCollapsedCond = g.SetNextWindowFocus = 0;
}

void ImwPlatformWindow::Paint()
{
	ImwWindowManager::GetInstance()->Paint(this);
}

bool ImwPlatformWindow::IsMain()
{
	return m_bMain;
}

void ImwPlatformWindow::Dock(ImwWindow* pWindow)
{
	m_pContainer->Dock(pWindow);
}

bool ImwPlatformWindow::UnDock(ImwWindow* pWindow)
{
	return m_pContainer->UnDock(pWindow);
}

ImwContainer* ImwPlatformWindow::GetContainer()
{
	return m_pContainer;
}

ImwContainer* ImwPlatformWindow::HasWindow(ImwWindow* pWindow)
{
	return m_pContainer->HasWindow(pWindow);
}

void ImwPlatformWindow::PaintContainer()
{
	m_pContainer->Paint();
}
<|endoftext|>


<commit_before>#ifndef __CONSOLE_HPP_INCLUDED
#define __CONSOLE_HPP_INCLUDED

#include "command_and_callback_struct.hpp"
#include "code/ylikuutio/callback_system/key_and_callback_struct.hpp"
#include "code/ylikuutio/callback_system/callback_parameter.hpp"
#include "code/ylikuutio/callback_system/callback_object.hpp"
#include "code/ylikuutio/callback_system/callback_engine.hpp"
#include "code/ylikuutio/common/any_value.hpp"
#include "code/ylikuutio/ontology/font2D.hpp"
#include "code/ylikuutio/common/globals.hpp"

// Include GLFW
#ifndef __GLFW3_H_INCLUDED
#define __GLFW3_H_INCLUDED
#include <glfw3.h>
#endif

// Include standard headers
#include <list>          // std::list
#include <stdint.h>      // uint32_t etc.
#include <string>        // std::string
#include <unordered_map> // std::unordered_map
#include <vector>        // std::vector

namespace console
{
    class Console
    {
        public:
            // constructor.
            Console(ConsoleStruct console_struct);

            // destructor.
            ~Console();

            void set_my_keypress_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer);
            void set_my_keyrelease_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer);
            void print_text(std::string text);
            void print_help();
            void draw_console();
            ontology::Universe* get_universe();

            // Public callbacks.

            // Action mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Action mode keypress callbacks begin here.

            static datatypes::AnyValue* enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keypress callbacks begin here.

            static datatypes::AnyValue* exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Public callbacks end here.

        private:
            static void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods);

            // Callbacks end here.

            void copy_historical_input_into_current_input();
            bool exit_console();
            void delete_character();
            void move_cursor_left();
            void move_cursor_right();
            void move_cursor_to_start_of_line();
            void move_cursor_to_end_of_line();
            void page_up();
            void page_down();
            void home();
            void end();

            std::list<char> current_input; // This is used for actual inputs.
            std::list<char>::iterator cursor_it;
            int32_t cursor_index;
            bool in_console;
            bool can_enter_console;
            bool can_exit_console;
            bool can_move_to_previous_input;
            bool can_move_to_next_input;
            bool can_backspace;
            bool can_enter_key;
            bool can_ctrl_c;
            bool is_left_control_pressed;
            bool is_right_control_pressed;
            bool is_left_alt_pressed;
            bool is_right_alt_pressed;
            bool is_left_shift_pressed;
            bool is_right_shift_pressed;

            std::vector<std::list<char>> command_history;
            std::vector<std::list<char>> console_history;

            bool in_historical_input;
            int32_t historical_input_i;
            std::list<char> temp_input;    // This is used for temporary storage of new input while modifying historical inputs.

            // These are related to keypress callbacks.
            std::vector<KeyAndCallbackStruct>** current_keypress_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keypress_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer;

            // These are related to keyrelease callbacks.
            std::vector<KeyAndCallbackStruct>** current_keyrelease_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keyrelease_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer;

            // This is a pointer to `std::unordered_map<std::string, bool>` that contains console command callbacks.
            std::unordered_map<std::string, ConsoleCommandCallback>* command_callback_map_pointer;

            // This is a pointer to `ontology::Universe`.
            ontology::Universe* universe_pointer;

            // This is a pointer to `font2D::Font2D` instance that is used for printing.
            ontology::Font2D* font2D_pointer;

            int32_t console_top_y;
            int32_t console_bottom_y;
            int32_t console_left_x;
            int32_t console_right_x;

            int32_t n_columns;
            int32_t n_rows;
    };
}

#endif
<commit_msg>`class Console`: `ontology::Universe* get_universe()` is now `private`.<commit_after>#ifndef __CONSOLE_HPP_INCLUDED
#define __CONSOLE_HPP_INCLUDED

#include "command_and_callback_struct.hpp"
#include "code/ylikuutio/callback_system/key_and_callback_struct.hpp"
#include "code/ylikuutio/callback_system/callback_parameter.hpp"
#include "code/ylikuutio/callback_system/callback_object.hpp"
#include "code/ylikuutio/callback_system/callback_engine.hpp"
#include "code/ylikuutio/common/any_value.hpp"
#include "code/ylikuutio/ontology/font2D.hpp"
#include "code/ylikuutio/common/globals.hpp"

// Include GLFW
#ifndef __GLFW3_H_INCLUDED
#define __GLFW3_H_INCLUDED
#include <glfw3.h>
#endif

// Include standard headers
#include <list>          // std::list
#include <stdint.h>      // uint32_t etc.
#include <string>        // std::string
#include <unordered_map> // std::unordered_map
#include <vector>        // std::vector

namespace map
{
    template <class T1>
        void print_keys_to_console(std::unordered_map<std::string, T1>* unordered_map_pointer, console::Console* console);
}

namespace console
{
    class Console
    {
        public:
            // constructor.
            Console(ConsoleStruct console_struct);

            // destructor.
            ~Console();

            void set_my_keypress_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer);
            void set_my_keyrelease_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer);
            void print_text(std::string text);
            void print_help();
            void draw_console();

            // Public callbacks.

            // Action mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Action mode keypress callbacks begin here.

            static datatypes::AnyValue* enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keypress callbacks begin here.

            static datatypes::AnyValue* exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Public callbacks end here.

            template <class T1>
                friend void map::print_keys_to_console(std::unordered_map<std::string, T1>* unordered_map_pointer, console::Console* console);

        private:
            static void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods);

            // Callbacks end here.

            ontology::Universe* get_universe();

            void copy_historical_input_into_current_input();
            bool exit_console();
            void delete_character();
            void move_cursor_left();
            void move_cursor_right();
            void move_cursor_to_start_of_line();
            void move_cursor_to_end_of_line();
            void page_up();
            void page_down();
            void home();
            void end();

            std::list<char> current_input; // This is used for actual inputs.
            std::list<char>::iterator cursor_it;
            int32_t cursor_index;
            bool in_console;
            bool can_enter_console;
            bool can_exit_console;
            bool can_move_to_previous_input;
            bool can_move_to_next_input;
            bool can_backspace;
            bool can_enter_key;
            bool can_ctrl_c;
            bool is_left_control_pressed;
            bool is_right_control_pressed;
            bool is_left_alt_pressed;
            bool is_right_alt_pressed;
            bool is_left_shift_pressed;
            bool is_right_shift_pressed;

            std::vector<std::list<char>> command_history;
            std::vector<std::list<char>> console_history;

            bool in_historical_input;
            int32_t historical_input_i;
            std::list<char> temp_input;    // This is used for temporary storage of new input while modifying historical inputs.

            // These are related to keypress callbacks.
            std::vector<KeyAndCallbackStruct>** current_keypress_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keypress_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer;

            // These are related to keyrelease callbacks.
            std::vector<KeyAndCallbackStruct>** current_keyrelease_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keyrelease_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer;

            // This is a pointer to `std::unordered_map<std::string, bool>` that contains console command callbacks.
            std::unordered_map<std::string, ConsoleCommandCallback>* command_callback_map_pointer;

            // This is a pointer to `ontology::Universe`.
            ontology::Universe* universe_pointer;

            // This is a pointer to `font2D::Font2D` instance that is used for printing.
            ontology::Font2D* font2D_pointer;

            int32_t console_top_y;
            int32_t console_bottom_y;
            int32_t console_left_x;
            int32_t console_right_x;

            int32_t n_columns;
            int32_t n_rows;
    };
}

#endif
<|endoftext|>


<commit_before>#ifndef __CONSOLE_HPP_INCLUDED
#define __CONSOLE_HPP_INCLUDED

#include "code/ylikuutio/callback_system/key_and_callback_struct.hpp"
#include "code/ylikuutio/callback_system/callback_parameter.hpp"
#include "code/ylikuutio/callback_system/callback_object.hpp"
#include "code/ylikuutio/callback_system/callback_engine.hpp"
#include "code/ylikuutio/common/any_value.hpp"
#include "code/ylikuutio/ontology/font2D.hpp"

// Include GLFW
#ifndef __GLFW3_H_INCLUDED
#define __GLFW3_H_INCLUDED
#include <glfw3.h>
#endif

// Include standard headers
#include <list>     // std::list
#include <stdint.h> // uint32_t etc.
#include <vector>   // std::vector

namespace console
{
    class Console
    {
        public:
            // constructor.
            Console(std::vector<KeyAndCallbackStruct>** current_keypress_callback_engine_vector_pointer_pointer,
                    std::vector<KeyAndCallbackStruct>** current_keyrelease_callback_engine_vector_pointer_pointer,
                    ontology::Font2D* text2D_pointer);

            // destructor.
            ~Console();

            void set_my_keypress_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer);
            void set_my_keyrelease_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer);
            void draw_console();

            // Callbacks.

            static void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods);

            // Action mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Action mode keypress callbacks begin here.

            static datatypes::AnyValue* enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keypress callbacks begin here.

            static datatypes::AnyValue* exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Callbacks end here.

        private:
            void copy_historical_input_into_current_input();
            bool exit_console();
            void delete_character();
            void move_cursor_left();
            void move_cursor_right();
            void move_cursor_to_start_of_line();
            void move_cursor_to_end_of_line();
            void page_up();
            void page_down();
            void home();
            void end();

            std::list<char> current_input; // This is used for actual inputs.
            std::list<char>::iterator cursor_it;
            uint32_t cursor_index;
            bool in_console;
            bool can_enter_console;
            bool can_exit_console;
            bool can_move_to_previous_input;
            bool can_move_to_next_input;
            bool can_backspace;
            bool can_enter_key;
            bool can_ctrl_c;
            bool is_left_control_pressed;
            bool is_right_control_pressed;
            bool is_left_alt_pressed;
            bool is_right_alt_pressed;
            bool is_left_shift_pressed;
            bool is_right_shift_pressed;

            std::vector<std::list<char>> command_history;
            bool in_historical_input;
            uint32_t historical_input_i;
            std::list<char> temp_input;    // This is used for temporary storage of new input while modifying historical inputs.

            // These are related to keypress callbacks.
            std::vector<KeyAndCallbackStruct>** current_keypress_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keypress_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer;

            // These are related to keyrelease callbacks.
            std::vector<KeyAndCallbackStruct>** current_keyrelease_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keyrelease_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer;

            // This is a pointer to `font2D::Font2D` instance that is used for printing.
            ontology::Font2D* text2D_pointer;
    };
}

#endif
<commit_msg>`static void charmods_callback` is now `private`.<commit_after>#ifndef __CONSOLE_HPP_INCLUDED
#define __CONSOLE_HPP_INCLUDED

#include "code/ylikuutio/callback_system/key_and_callback_struct.hpp"
#include "code/ylikuutio/callback_system/callback_parameter.hpp"
#include "code/ylikuutio/callback_system/callback_object.hpp"
#include "code/ylikuutio/callback_system/callback_engine.hpp"
#include "code/ylikuutio/common/any_value.hpp"
#include "code/ylikuutio/ontology/font2D.hpp"

// Include GLFW
#ifndef __GLFW3_H_INCLUDED
#define __GLFW3_H_INCLUDED
#include <glfw3.h>
#endif

// Include standard headers
#include <list>     // std::list
#include <stdint.h> // uint32_t etc.
#include <vector>   // std::vector

namespace console
{
    class Console
    {
        public:
            // constructor.
            Console(std::vector<KeyAndCallbackStruct>** current_keypress_callback_engine_vector_pointer_pointer,
                    std::vector<KeyAndCallbackStruct>** current_keyrelease_callback_engine_vector_pointer_pointer,
                    ontology::Font2D* text2D_pointer);

            // destructor.
            ~Console();

            void set_my_keypress_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer);
            void set_my_keyrelease_callback_engine_vector_pointer(std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer);
            void draw_console();

            // Public callbacks.

            // Action mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Action mode keypress callbacks begin here.

            static datatypes::AnyValue* enter_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keyrelease callbacks begin here.

            static datatypes::AnyValue* enable_exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* release_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enable_ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Console mode keypress callbacks begin here.

            static datatypes::AnyValue* exit_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_control_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_alt_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_left_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* press_right_shift_in_console(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_previous_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* move_to_next_input(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* backspace(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* enter_key(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            static datatypes::AnyValue* ctrl_c(
                    callback_system::CallbackEngine*,
                    callback_system::CallbackObject*,
                    std::vector<callback_system::CallbackParameter*>&,
                    console::Console* console);

            // Public callbacks end here.

        private:
            static void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods);

            // Callbacks end here.

            void copy_historical_input_into_current_input();
            bool exit_console();
            void delete_character();
            void move_cursor_left();
            void move_cursor_right();
            void move_cursor_to_start_of_line();
            void move_cursor_to_end_of_line();
            void page_up();
            void page_down();
            void home();
            void end();

            std::list<char> current_input; // This is used for actual inputs.
            std::list<char>::iterator cursor_it;
            uint32_t cursor_index;
            bool in_console;
            bool can_enter_console;
            bool can_exit_console;
            bool can_move_to_previous_input;
            bool can_move_to_next_input;
            bool can_backspace;
            bool can_enter_key;
            bool can_ctrl_c;
            bool is_left_control_pressed;
            bool is_right_control_pressed;
            bool is_left_alt_pressed;
            bool is_right_alt_pressed;
            bool is_left_shift_pressed;
            bool is_right_shift_pressed;

            std::vector<std::list<char>> command_history;
            bool in_historical_input;
            uint32_t historical_input_i;
            std::list<char> temp_input;    // This is used for temporary storage of new input while modifying historical inputs.

            // These are related to keypress callbacks.
            std::vector<KeyAndCallbackStruct>** current_keypress_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keypress_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keypress_callback_engine_vector_pointer;

            // These are related to keyrelease callbacks.
            std::vector<KeyAndCallbackStruct>** current_keyrelease_callback_engine_vector_pointer_pointer;
            std::vector<KeyAndCallbackStruct>* previous_keyrelease_callback_engine_vector_pointer;
            std::vector<KeyAndCallbackStruct>* my_keyrelease_callback_engine_vector_pointer;

            // This is a pointer to `font2D::Font2D` instance that is used for printing.
            ontology::Font2D* text2D_pointer;
    };
}

#endif
<|endoftext|>


<commit_before>#ifndef CUSTOMPROJECTION_HH
#define CUSTOMPROJECTION_HH

#include <dune/fem/quadrature/cachingquadrature.hh>
#include <dune/fem/operator/common/operator.hh>
#include <dune/fem/function/common/discretefunction.hh>
#include <dune/fem/function/common/discretefunctionadapter.hh>
#include <dune/fem/operator/1order/localmassmatrix.hh>

namespace Stuff {

/** A custom projection of an analytical function that uses a non-standard evalute signature:\n

template < class IntersectionIteratorType >\n
		void evaluate( const DomainType& arg, RangeType& ret, const IntersectionIteratorType& faceIter ) const
\n
	\note example being our boundary functions
	\note output currently somewhat meaningless
	\see analyticaldata.hh
**/
class CustomProjection {

public:
	template < class OriginFunctionType, class DestinationFunctionType >
	static void project (const OriginFunctionType& f, DestinationFunctionType& discFunc)
	{
		const double time = 0.0;
		typedef typename DestinationFunctionType::FunctionSpaceType
			DiscreteFunctionSpace;
		typedef typename DiscreteFunctionSpace::GridPartType
			GridPart;
		typedef typename GridPart::template Codim< 0 >::IteratorType
			EntityIteratorType;
		typedef typename GridPart::GridType::template Codim< 0 >::Entity
			EntityType;
		typedef typename GridPart::IntersectionIteratorType
			IntersectionIteratorType;
		typedef typename IntersectionIteratorType::EntityPointer
			EntityPointer;
		typedef typename DestinationFunctionType::LocalFunctionType
			LocalFunctionType;
		typedef Dune::CachingQuadrature< GridPart, 1 >
			FaceQuadratureType;
		typedef typename DiscreteFunctionSpace::BaseFunctionSetType
			BaseFunctionSetType;
		typedef typename DiscreteFunctionSpace::RangeType
			RangeType;
		const DiscreteFunctionSpace& space_ = discFunc.space();
		const GridPart& gridPart_ = space_.gridPart();
		RangeType phi (0.0);
		EntityIteratorType entityItEndLog = space_.end();
		for (   EntityIteratorType it = space_.begin();
				it != entityItEndLog;
				++it )
		{
			EntityType& e = *it;
			LocalFunctionType lf = discFunc.localFunction( e );
			BaseFunctionSetType baseFunctionset = space_.baseFunctionSet( *it );
			unsigned int intersection_count = 0;
			IntersectionIteratorType intItEnd = gridPart_.iend( *it );
			for (   IntersectionIteratorType intIt = gridPart_.ibegin( *it );
					intIt != intItEnd;
					++intIt ) {
				intersection_count++;
				FaceQuadratureType faceQuadrature( gridPart_,
												   *intIt,
												   ( 4 * space_.order() ) + 1,
												   FaceQuadratureType::INSIDE );
				typename DestinationFunctionType::RangeType ret;
				for ( size_t qP = 0; qP < faceQuadrature.nop(); ++qP ) {
					const double intel =
						 faceQuadrature.weight(qP) * e.geometry().integrationElement( faceQuadrature.point(qP) ); // general case

					if ( intIt->boundary() )
					{
						f.evaluate( faceQuadrature.point(qP), ret, *intIt );

						for ( int i = 0; i < baseFunctionset.numBaseFunctions(); ++i ) {
							baseFunctionset.evaluate(i, faceQuadrature[qP], phi);
							lf[i] += intel * (ret * phi) ;
						}
					}
				}
			}
		}
	}
};
}//end namespace Stuff

namespace Dune {
//! basically the fem L2Projection with a function evaluate that converts between compatible types instead of failing
class BetterL2Projection {
public:
	template <class FunctionImp, class DiscreteFunctionImp>
	static void project(const FunctionImp& func,
								DiscreteFunctionImp& discFunc,
								int polOrd = -1)
	{
	  typedef typename DiscreteFunctionImp::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
	  typedef typename DiscreteFunctionImp::LocalFunctionType LocalFuncType;
	  typedef typename DiscreteFunctionSpaceType::Traits::GridPartType GridPartType;
	  typedef typename DiscreteFunctionSpaceType::Traits::IteratorType Iterator;
	  typedef typename DiscreteFunctionSpaceType::BaseFunctionSetType BaseFunctionSetType ;
	  typedef typename GridPartType::GridType GridType;

	  typedef typename FunctionImp::LocalFunctionType LocalFType;

	  typename DiscreteFunctionSpaceType::RangeType ret (0.0);
	  typename DiscreteFunctionSpaceType::RangeType phi (0.0);
	  const DiscreteFunctionSpaceType& space =  discFunc.space();

	  // type of quadrature
	  typedef CachingQuadrature<GridPartType,0> QuadratureType;
	  // type of local mass matrix
	  typedef LocalDGMassMatrix< DiscreteFunctionSpaceType, QuadratureType > LocalMassMatrixType;

	  const int quadOrd = (polOrd == -1) ? (2 * space.order()) : polOrd;

	  // create local mass matrix object
	  LocalMassMatrixType massMatrix( space, quadOrd );

	  // check whether geometry mappings are affine or not
	  const bool affineMapping = massMatrix.affine();

	  // clear destination
	  discFunc.clear();

	  const Iterator endit = space.end();
	  for(Iterator it = space.begin(); it != endit ; ++it)
	  {
		// get entity
		const typename GridType::template Codim<0>::Entity& en = *it;
		// get geometry
		const typename GridType::template Codim<0>::Geometry& geo = en.geometry();

		// get quadrature
		QuadratureType quad(en, quadOrd);

		// get local function of destination
		LocalFuncType lf = discFunc.localFunction(en);
		// get local function of argument
		const LocalFType f = func.localFunction(en);

		// get base function set
		const BaseFunctionSetType & baseset = lf.baseFunctionSet();

		const int quadNop = quad.nop();
		const int numDofs = lf.numDofs();

		for(int qP = 0; qP < quadNop ; ++qP)
		{
		  const double intel = (affineMapping) ?
			   quad.weight(qP) : // affine case
			   quad.weight(qP) * geo.integrationElement( quad.point(qP) ); // general case

		  // evaluate function
		  typename FunctionImp::DiscreteFunctionSpaceType::RangeType
				  dummy;
		  f.evaluate(quad[qP], dummy);
		  ret =dummy;

		  // do projection
		  for(int i=0; i<numDofs; ++i)
		  {
			baseset.evaluate(i, quad[qP], phi);
			lf[i] += intel * (ret * phi) ;
		  }
		}

		// in case of non-linear mapping apply inverse
		if ( ! affineMapping )
		{
		  massMatrix.applyInverse( en, lf );
		}
	  }
	}
};

}//end namespace Dune
#endif // CUSTOMPROJECTION_HH
<commit_msg>improved projection class that is time aware on demand<commit_after>#ifndef CUSTOMPROJECTION_HH
#define CUSTOMPROJECTION_HH

#include <dune/fem/quadrature/cachingquadrature.hh>
#include <dune/fem/operator/common/operator.hh>
#include <dune/fem/function/common/discretefunction.hh>
#include <dune/fem/function/common/discretefunctionadapter.hh>
#include <dune/fem/operator/1order/localmassmatrix.hh>

namespace Stuff {

/** A custom projection of an analytical function that uses a non-standard evalute signature:\n

template < class IntersectionIteratorType >\n
		void evaluate( const DomainType& arg, RangeType& ret, const IntersectionIteratorType& faceIter ) const
\n
	\note example being our boundary functions
	\note output currently somewhat meaningless
	\see analyticaldata.hh
**/
class CustomProjection {

public:
	template < class OriginFunctionType, class DestinationFunctionType >
	static void project (const OriginFunctionType& f, DestinationFunctionType& discFunc)
	{
		const double time = 0.0;
		typedef typename DestinationFunctionType::FunctionSpaceType
			DiscreteFunctionSpace;
		typedef typename DiscreteFunctionSpace::GridPartType
			GridPart;
		typedef typename GridPart::template Codim< 0 >::IteratorType
			EntityIteratorType;
		typedef typename GridPart::GridType::template Codim< 0 >::Entity
			EntityType;
		typedef typename GridPart::IntersectionIteratorType
			IntersectionIteratorType;
		typedef typename IntersectionIteratorType::EntityPointer
			EntityPointer;
		typedef typename DestinationFunctionType::LocalFunctionType
			LocalFunctionType;
		typedef Dune::CachingQuadrature< GridPart, 1 >
			FaceQuadratureType;
		typedef typename DiscreteFunctionSpace::BaseFunctionSetType
			BaseFunctionSetType;
		typedef typename DiscreteFunctionSpace::RangeType
			RangeType;
		const DiscreteFunctionSpace& space_ = discFunc.space();
		const GridPart& gridPart_ = space_.gridPart();
		RangeType phi (0.0);
		EntityIteratorType entityItEndLog = space_.end();
		for (   EntityIteratorType it = space_.begin();
				it != entityItEndLog;
				++it )
		{
			EntityType& e = *it;
			LocalFunctionType lf = discFunc.localFunction( e );
			BaseFunctionSetType baseFunctionset = space_.baseFunctionSet( *it );
			unsigned int intersection_count = 0;
			IntersectionIteratorType intItEnd = gridPart_.iend( *it );
			for (   IntersectionIteratorType intIt = gridPart_.ibegin( *it );
					intIt != intItEnd;
					++intIt ) {
				intersection_count++;
				FaceQuadratureType faceQuadrature( gridPart_,
												   *intIt,
												   ( 4 * space_.order() ) + 1,
												   FaceQuadratureType::INSIDE );
				typename DestinationFunctionType::RangeType ret;
				for ( size_t qP = 0; qP < faceQuadrature.nop(); ++qP ) {
					const double intel =
						 faceQuadrature.weight(qP) * e.geometry().integrationElement( faceQuadrature.point(qP) ); // general case

					if ( intIt->boundary() )
					{
						f.evaluate( faceQuadrature.point(qP), ret, *intIt );

						for ( int i = 0; i < baseFunctionset.numBaseFunctions(); ++i ) {
							baseFunctionset.evaluate(i, faceQuadrature[qP], phi);
							lf[i] += intel * (ret * phi) ;
						}
					}
				}
			}
		}
	}
};
}//end namespace Stuff

namespace Dune {
//! basically the fem L2Projection with a function evaluate that converts between compatible types instead of failing
class BetterL2Projection {
protected:
	template < class FunctionType >
	struct DefaultEvaluationFunctor;
public:
	template <class FunctionImp, class DiscreteFunctionImp>
	static void project(const FunctionImp& func,
								DiscreteFunctionImp& discFunc,
								int polOrd = -1)
	{
		CompileTimeChecker< !Conversion<FunctionImp, IsDiscreteFunction> ::exists > TimeAwareL2Projection_not_implemented_for_discrete_source_functions;
		DefaultEvaluationFunctor< FunctionImp > functor( func );
		projectCommon( functor, discFunc, polOrd );
	}
	template < class TimeProviderType, class FunctionImp, class DiscreteFunctionImp>
	static void project(	const TimeProviderType& timeProvider,
							const FunctionImp& func,
							DiscreteFunctionImp& discFunc,
							int polOrd = -1)
	{
		CompileTimeChecker< !Conversion<FunctionImp, IsDiscreteFunction> ::exists > TimeAwareL2Projection_not_implemented_for_discrete_source_functions;
		TimeEvaluationFunctor< FunctionImp, TimeProviderType > functor( func, timeProvider );
		projectCommon( functor, discFunc, polOrd );
	}

protected:
	template < class FunctionType >
	struct DefaultEvaluationFunctor
	{
		const FunctionType& function_;
		DefaultEvaluationFunctor( const FunctionType& function )
			:function_( function )
		{}

		void evaluate( const typename FunctionType::DomainType& arg, typename FunctionType::RangeType& ret ) const
		{
			function_.evaluate( arg, ret );
		}
	};
	template < class FunctionType, class TimeProviderType >
	struct TimeEvaluationFunctor
	{
		const FunctionType& function_;
		const TimeProviderType& timeProvider_;
		TimeEvaluationFunctor(	const FunctionType& function,
								const TimeProviderType& timeProvider)
			:function_( function ),
			timeProvider_( timeProvider )
		{}
		void evaluate( const typename FunctionType::DomainType& arg, typename FunctionType::RangeType& ret ) const
		{
			function_.evaluate( timeProvider_.subTime(), arg, ret );
		}
	};

	template < class DiscreteFunctionImp, class EvaluationFunctorType >
	static void projectCommon(	const EvaluationFunctorType& evalutionFunctor,
								DiscreteFunctionImp& discFunc,
								int polOrd = -1)
	{
	  typedef typename DiscreteFunctionImp::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
	  typedef typename DiscreteFunctionImp::LocalFunctionType LocalFuncType;
	  typedef typename DiscreteFunctionSpaceType::Traits::GridPartType GridPartType;
	  typedef typename DiscreteFunctionSpaceType::Traits::IteratorType Iterator;
	  typedef typename DiscreteFunctionSpaceType::BaseFunctionSetType BaseFunctionSetType ;
	  typedef typename GridPartType::GridType GridType;

//	  typedef typename FunctionImp::LocalFunctionType LocalFType;

	  typename DiscreteFunctionSpaceType::RangeType ret (0.0);
	  typename DiscreteFunctionSpaceType::RangeType phi (0.0);
	  const DiscreteFunctionSpaceType& space =  discFunc.space();

	  // type of quadrature
	  typedef CachingQuadrature<GridPartType,0> QuadratureType;
	  // type of local mass matrix
	  typedef LocalDGMassMatrix< DiscreteFunctionSpaceType, QuadratureType > LocalMassMatrixType;

	  const int quadOrd = (polOrd == -1) ? (2 * space.order()) : polOrd;

	  // create local mass matrix object
	  LocalMassMatrixType massMatrix( space, quadOrd );

	  // check whether geometry mappings are affine or not
	  const bool affineMapping = massMatrix.affine();

	  // clear destination
	  discFunc.clear();

	  const Iterator endit = space.end();
	  for(Iterator it = space.begin(); it != endit ; ++it)
	  {
		// get entity
		const typename GridType::template Codim<0>::Entity& en = *it;
		// get geometry
		const typename GridType::template Codim<0>::Geometry& geo = en.geometry();

		// get quadrature
		QuadratureType quad(en, quadOrd);

		// get local function of destination
		LocalFuncType lf = discFunc.localFunction(en);

		// get base function set
		const BaseFunctionSetType & baseset = lf.baseFunctionSet();

		const int quadNop = quad.nop();
		const int numDofs = lf.numDofs();

		for(int qP = 0; qP < quadNop ; ++qP)
		{
		  const double intel = (affineMapping) ?
			   quad.weight(qP) : // affine case
			   quad.weight(qP) * geo.integrationElement( quad.point(qP) ); // general case

		  // evaluate function
		  typename DiscreteFunctionSpaceType::DomainType x = geo.global( quad.point( qP ) );
		  evalutionFunctor.evaluate( x, ret );

		  // do projection
		  for(int i=0; i<numDofs; ++i)
		  {
			baseset.evaluate(i, quad[qP], phi);
			lf[i] += intel * (ret * phi) ;
		  }
		}

		// in case of non-linear mapping apply inverse
		if ( ! affineMapping )
		{
		  massMatrix.applyInverse( en, lf );
		}
	  }
	}
};

}//end namespace Dune
#endif // CUSTOMPROJECTION_HH
<|endoftext|>


<commit_before>#include "exec.h"

void Execute::cachePut(const std::string& key, const std::string value) {
	if (!this->cache) {
		this->cache = (art_tree *)malloc(sizeof(art_tree));
		art_tree_init(this->cache);
	}

	art_insert(this->cache, (unsigned char *)key.c_str(), key.size(), new std::string(value));
}

void Execute::cacheDelete(const std::string& key) {
	if (!this->cache)
		return;

	art_delete(this->cache, (unsigned char *)key.c_str(), key.size());
}

std::string Execute::cacheGet(const std::string& key) {
	if (!this->cache)
		return "";

	void *result = art_search(this->cache, (unsigned char *)key.c_str(), key.size());
	if (!result)
		return "";

	return *static_cast<std::string *>(result);
}
<commit_msg>Ignore unit<commit_after>#ifdef CACHE
#include "exec.h"

void Execute::cachePut(const std::string& key, const std::string value) {
	if (!this->cache) {
		this->cache = (art_tree *)malloc(sizeof(art_tree));
		art_tree_init(this->cache);
	}

	art_insert(this->cache, (unsigned char *)key.c_str(), key.size(), new std::string(value));
}

void Execute::cacheDelete(const std::string& key) {
	if (!this->cache)
		return;

	art_delete(this->cache, (unsigned char *)key.c_str(), key.size());
}

std::string Execute::cacheGet(const std::string& key) {
	if (!this->cache)
		return "";

	void *result = art_search(this->cache, (unsigned char *)key.c_str(), key.size());
	if (!result)
		return "";

	return *static_cast<std::string *>(result);
}
#endif
<|endoftext|>


<commit_before>// Copyright (c) 2009 - Decho Corp.

#include "mordor/pch.h"

#include "timer.h"

#include <algorithm>
#include <vector>

#include "assert.h"
#include "exception.h"
#include "version.h"

#ifdef OSX
 #include <mach/mach_time.h>
#elif defined(WINDOWS)
 #include <windows.h>  // for LARGE_INTEGER, QueryPerformanceFrequency()
#else
 #include <sys/time.h>
 #include <time.h>
#endif

namespace Mordor {

#ifdef WINDOWS
static unsigned long long queryFrequency()
{
    LARGE_INTEGER frequency;
    BOOL bRet = QueryPerformanceFrequency(&frequency);
    MORDOR_ASSERT(bRet);
    return (unsigned long long)frequency.QuadPart;
}

unsigned long long g_frequency = queryFrequency();
#elif defined (OSX)
static mach_timebase_info_data_t queryTimebase()
{
    mach_timebase_info_data_t timebase;
    mach_timebase_info(&timebase);
    return timebase;
}
mach_timebase_info_data_t g_timebase = queryTimebase();
#endif

unsigned long long
TimerManager::now()
{
#ifdef WINDOWS
    LARGE_INTEGER count;
    if (!QueryPerformanceCounter(&count))
        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("QueryPerformanceCounter");
    unsigned long long countUll = (unsigned long long)count.QuadPart;
    return countUll * 1000000 / g_frequency;
#elif defined(OSX)
    unsigned long long absoluteTime = mach_absolute_time();
    return absoluteTime * g_timebase.numer / g_timebase.denom / 1000;
#else
    struct timespec ts;

    if (clock_gettime(CLOCK_MONOTONIC, &ts))
        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("clock_gettime");
    return ts.tv_sec * 1000000ull + ts.tv_nsec / 1000;
#endif
}

Timer::Timer(unsigned long long us, boost::function<void ()> dg, bool recurring,
             TimerManager *manager)
    : m_us(us),
      m_dg(dg),
      m_recurring(recurring),
      m_manager(manager)
{
    MORDOR_ASSERT(m_dg);
    m_next = TimerManager::now() + m_us;
}

Timer::Timer(unsigned long long next)
    : m_next(next)
{}

void
Timer::cancel()
{
    if (m_next != 0) {
        boost::mutex::scoped_lock lock(m_manager->m_mutex);
        std::set<Timer::ptr, Timer::Comparator>::iterator it =
            m_manager->m_timers.find(shared_from_this());
        MORDOR_ASSERT(it != m_manager->m_timers.end());
        m_next = 0;
        m_manager->m_timers.erase(it);
    }
}

TimerManager::~TimerManager()
{
#ifndef NDEBUG
    boost::mutex::scoped_lock lock(m_mutex);
    MORDOR_ASSERT(m_timers.empty());
#endif
}

Timer::ptr
TimerManager::registerTimer(unsigned long long us, boost::function<void ()> dg,
        bool recurring)
{
    bool atFront;
    return registerTimer(us, dg, recurring, atFront);
}

Timer::ptr
TimerManager::registerTimer(unsigned long long us, boost::function<void ()> dg,
        bool recurring, bool &atFront)
{
    Timer::ptr result(new Timer(us, dg, recurring, this));
    boost::mutex::scoped_lock lock(m_mutex);
    atFront = (m_timers.insert(result).first == m_timers.begin());
    return result;
}

unsigned long long
TimerManager::nextTimer()
{
    boost::mutex::scoped_lock lock(m_mutex);
    if (m_timers.empty())
        return ~0ull;
    const Timer::ptr &next = *m_timers.begin();
    unsigned long long nowUs = now();
    if (nowUs >= next->m_next)
        return 0;
    return next->m_next - nowUs;
}

static
void delete_nothing(Timer *t)
{}

void
TimerManager::processTimers()
{
    std::vector<Timer::ptr> expired;
    unsigned long long nowUs = now();
    {
        boost::mutex::scoped_lock lock(m_mutex);
        if (m_timers.empty() || (*m_timers.begin())->m_next > nowUs)
            return;
        Timer nowTimer(nowUs);
        Timer::ptr nowTimerPtr(&nowTimer, &delete_nothing);
        // Find all timers that are expired
        std::set<Timer::ptr, Timer::Comparator>::iterator it =
            m_timers.lower_bound(nowTimerPtr);
        while (it != m_timers.end() && (*it)->m_next == nowUs ) ++it;
        // Copy to expired, remove from m_timers;
        expired.insert(expired.begin(), m_timers.begin(), it);
        m_timers.erase(m_timers.begin(), it);
        // Look at expired timers and re-register recurring timers
        // (while under the same lock)
        for (std::vector<Timer::ptr>::iterator it2(expired.begin());
            it2 != expired.end();
            ++it2) {
            Timer::ptr &timer = *it2;
            if (timer->m_recurring) {
                timer->m_next = nowUs + timer->m_us;
                m_timers.insert(timer);
            }
        }
    }
    // Run the callbacks for each expired timer (not under a lock)
    for (std::vector<Timer::ptr>::iterator it2(expired.begin());
        it2 != expired.end();
        ++it2) {
        Timer::ptr &timer = *it2;
        // Make sure someone else hasn't cancelled us
        // TODO: need a per-timer lock for this?
        if (timer->m_next != 0) {
            if (!timer->m_recurring) timer->m_next = 0;
            timer->m_dg();
        }
    }
}

bool
Timer::Comparator::operator()(const Timer::ptr &lhs,
                              const Timer::ptr &rhs) const
{
    // Order NULL before everything else
    if (!lhs && !rhs)
        return false;
    if (!lhs)
        return true;
    if (!rhs)
        return false;
    // Order primarily on m_next
    if (lhs->m_next < rhs->m_next)
        return true;
    if (rhs->m_next < lhs->m_next)
        return false;
    // Order by raw pointer for equivalent timeout values
    return lhs.get() < rhs.get();
}

}
<commit_msg>Logging for timers<commit_after>// Copyright (c) 2009 - Decho Corp.

#include "mordor/pch.h"

#include "timer.h"

#include <algorithm>
#include <vector>

#include "assert.h"
#include "exception.h"
#include "log.h"
#include "version.h"

#ifdef OSX
 #include <mach/mach_time.h>
#elif defined(WINDOWS)
 #include <windows.h>  // for LARGE_INTEGER, QueryPerformanceFrequency()
#else
 #include <sys/time.h>
 #include <time.h>
#endif

namespace Mordor {

static Logger::ptr g_log = Log::lookup("mordor:timer");

#ifdef WINDOWS
static unsigned long long queryFrequency()
{
    LARGE_INTEGER frequency;
    BOOL bRet = QueryPerformanceFrequency(&frequency);
    MORDOR_ASSERT(bRet);
    return (unsigned long long)frequency.QuadPart;
}

unsigned long long g_frequency = queryFrequency();
#elif defined (OSX)
static mach_timebase_info_data_t queryTimebase()
{
    mach_timebase_info_data_t timebase;
    mach_timebase_info(&timebase);
    return timebase;
}
mach_timebase_info_data_t g_timebase = queryTimebase();
#endif

unsigned long long
TimerManager::now()
{
#ifdef WINDOWS
    LARGE_INTEGER count;
    if (!QueryPerformanceCounter(&count))
        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("QueryPerformanceCounter");
    unsigned long long countUll = (unsigned long long)count.QuadPart;
    return countUll * 1000000 / g_frequency;
#elif defined(OSX)
    unsigned long long absoluteTime = mach_absolute_time();
    return absoluteTime * g_timebase.numer / g_timebase.denom / 1000;
#else
    struct timespec ts;

    if (clock_gettime(CLOCK_MONOTONIC, &ts))
        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("clock_gettime");
    return ts.tv_sec * 1000000ull + ts.tv_nsec / 1000;
#endif
}

Timer::Timer(unsigned long long us, boost::function<void ()> dg, bool recurring,
             TimerManager *manager)
    : m_us(us),
      m_dg(dg),
      m_recurring(recurring),
      m_manager(manager)
{
    MORDOR_ASSERT(m_dg);
    m_next = TimerManager::now() + m_us;
}

Timer::Timer(unsigned long long next)
    : m_next(next)
{}

void
Timer::cancel()
{
    MORDOR_LOG_VERBOSE(g_log) << this << " cancel";
    if (m_next != 0) {
        boost::mutex::scoped_lock lock(m_manager->m_mutex);
        std::set<Timer::ptr, Timer::Comparator>::iterator it =
            m_manager->m_timers.find(shared_from_this());
        MORDOR_ASSERT(it != m_manager->m_timers.end());
        m_next = 0;
        m_manager->m_timers.erase(it);
    }
}

TimerManager::~TimerManager()
{
#ifndef NDEBUG
    boost::mutex::scoped_lock lock(m_mutex);
    MORDOR_ASSERT(m_timers.empty());
#endif
}

Timer::ptr
TimerManager::registerTimer(unsigned long long us, boost::function<void ()> dg,
        bool recurring)
{
    bool atFront;
    return registerTimer(us, dg, recurring, atFront);
}

Timer::ptr
TimerManager::registerTimer(unsigned long long us, boost::function<void ()> dg,
        bool recurring, bool &atFront)
{
    Timer::ptr result(new Timer(us, dg, recurring, this));
    {
        boost::mutex::scoped_lock lock(m_mutex);
        atFront = (m_timers.insert(result).first == m_timers.begin());
    }
    MORDOR_LOG_VERBOSE(g_log) << result.get() << " registerTimer(" << us
        << ", " << us << ", " << recurring << "): " << atFront;
    return result;
}

unsigned long long
TimerManager::nextTimer()
{
    boost::mutex::scoped_lock lock(m_mutex);
    if (m_timers.empty()) {
        MORDOR_LOG_VERBOSE(g_log) << this << " nextTimer(): ~0ull";
        return ~0ull;
    }
    const Timer::ptr &next = *m_timers.begin();
    unsigned long long nowUs = now();
    unsigned long long result;
    if (nowUs >= next->m_next)
        result = 0;
    else
        result = next->m_next - nowUs;
    MORDOR_LOG_VERBOSE(g_log) << this << " nextTimer(): " << result;
    return result;
}

static
void delete_nothing(Timer *t)
{}

void
TimerManager::processTimers()
{
    std::vector<Timer::ptr> expired;
    unsigned long long nowUs = now();
    {
        boost::mutex::scoped_lock lock(m_mutex);
        if (m_timers.empty() || (*m_timers.begin())->m_next > nowUs)
            return;
        Timer nowTimer(nowUs);
        Timer::ptr nowTimerPtr(&nowTimer, &delete_nothing);
        // Find all timers that are expired
        std::set<Timer::ptr, Timer::Comparator>::iterator it =
            m_timers.lower_bound(nowTimerPtr);
        while (it != m_timers.end() && (*it)->m_next == nowUs ) ++it;
        // Copy to expired, remove from m_timers;
        expired.insert(expired.begin(), m_timers.begin(), it);
        m_timers.erase(m_timers.begin(), it);
        // Look at expired timers and re-register recurring timers
        // (while under the same lock)
        for (std::vector<Timer::ptr>::iterator it2(expired.begin());
            it2 != expired.end();
            ++it2) {
            Timer::ptr &timer = *it2;
            if (timer->m_recurring) {
                timer->m_next = nowUs + timer->m_us;
                m_timers.insert(timer);
            }
        }
    }
    // Run the callbacks for each expired timer (not under a lock)
    for (std::vector<Timer::ptr>::iterator it2(expired.begin());
        it2 != expired.end();
        ++it2) {
        Timer::ptr &timer = *it2;
        // Make sure someone else hasn't cancelled us
        // TODO: need a per-timer lock for this?
        if (timer->m_next != 0) {
            if (!timer->m_recurring) timer->m_next = 0;
            MORDOR_LOG_VERBOSE(g_log) << timer.get() << " dg()";
            timer->m_dg();
        }
    }
}

bool
Timer::Comparator::operator()(const Timer::ptr &lhs,
                              const Timer::ptr &rhs) const
{
    // Order NULL before everything else
    if (!lhs && !rhs)
        return false;
    if (!lhs)
        return true;
    if (!rhs)
        return false;
    // Order primarily on m_next
    if (lhs->m_next < rhs->m_next)
        return true;
    if (rhs->m_next < lhs->m_next)
        return false;
    // Order by raw pointer for equivalent timeout values
    return lhs.get() < rhs.get();
}

}
<|endoftext|>


<commit_before>// This sends a firmware file to the drive

#include <stdio.h>
#import <fstream>

#include "protobufutil/message_stream.h"

#include "connection_options.h"
#include "hmac_provider.h"
#include "kinetic_connection_factory.h"
#include "value_factory.h"

using com::seagate::kinetic::HmacProvider;
using com::seagate::kinetic::proto::Message;
using com::seagate::kinetic::proto::Message_MessageType_GET;
using com::seagate::kinetic::proto::Message_Algorithm_SHA1;
using com::seagate::kinetic::ValueFactory;
using kinetic::KineticConnection;
using kinetic::KineticConnectionFactory;
using kinetic::Status;
using kinetic::KineticRecord;
using palominolabs::protobufutil::MessageStreamFactory;

int main(int argc, char* argv[]) {

    if (argc != 3) {
        printf("Usage: %s <host> <input file name>\n", argv[0]);
        return 1;
    }

    const char* host = argv[1];
    const char* input_file_name = argv[2];

    kinetic::ConnectionOptions options;
    options.host = host;
    options.port = 8123;
    options.user_id = 1;
    options.hmac_key = "asdfasdf";

    HmacProvider hmac_provider;
    ValueFactory value_factory;
    MessageStreamFactory message_stream_factory(NULL, value_factory);
    kinetic::KineticConnectionFactory kinetic_connection_factory(hmac_provider,
            message_stream_factory);

    kinetic::KineticConnection* kinetic_connection;
    if (!kinetic_connection_factory.NewConnection(options, &kinetic_connection).ok()) {
        printf("Unable to connect\n");
        return 1;
    }

    std::ifstream in(input_file_name, std::ios::in | std::ios::binary);
    std::ostringstream contents;
    if (in.fail()) {
        printf("Unable to read file\n");
        return 1;
    } else {
        contents << in.rdbuf();
    }

    if (!kinetic_connection->FirmwareUpdate(contents.str()).ok()) {
        printf("Unable to send firmware\n");
        return 1;
    }

    printf("Done!\n");

    return 0;
}
<commit_msg>use #include instead of #import<commit_after>// This sends a firmware file to the drive

#include <stdio.h>
#include <fstream>

#include "protobufutil/message_stream.h"

#include "connection_options.h"
#include "hmac_provider.h"
#include "kinetic_connection_factory.h"
#include "value_factory.h"

using com::seagate::kinetic::HmacProvider;
using com::seagate::kinetic::proto::Message;
using com::seagate::kinetic::proto::Message_MessageType_GET;
using com::seagate::kinetic::proto::Message_Algorithm_SHA1;
using com::seagate::kinetic::ValueFactory;
using kinetic::KineticConnection;
using kinetic::KineticConnectionFactory;
using kinetic::Status;
using kinetic::KineticRecord;
using palominolabs::protobufutil::MessageStreamFactory;

int main(int argc, char* argv[]) {

    if (argc != 3) {
        printf("Usage: %s <host> <input file name>\n", argv[0]);
        return 1;
    }

    const char* host = argv[1];
    const char* input_file_name = argv[2];

    kinetic::ConnectionOptions options;
    options.host = host;
    options.port = 8123;
    options.user_id = 1;
    options.hmac_key = "asdfasdf";

    HmacProvider hmac_provider;
    ValueFactory value_factory;
    MessageStreamFactory message_stream_factory(NULL, value_factory);
    kinetic::KineticConnectionFactory kinetic_connection_factory(hmac_provider,
            message_stream_factory);

    kinetic::KineticConnection* kinetic_connection;
    if (!kinetic_connection_factory.NewConnection(options, &kinetic_connection).ok()) {
        printf("Unable to connect\n");
        return 1;
    }

    std::ifstream in(input_file_name, std::ios::in | std::ios::binary);
    std::ostringstream contents;
    if (in.fail()) {
        printf("Unable to read file\n");
        return 1;
    } else {
        contents << in.rdbuf();
    }

    if (!kinetic_connection->FirmwareUpdate(contents.str()).ok()) {
        printf("Unable to send firmware\n");
        return 1;
    }

    printf("Done!\n");

    return 0;
}
<|endoftext|>